package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private ShoppingMapper shoppingMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void cleanCart() {
        shoppingMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public List<ShoppingCart> findShoppingCaryList() {
        ShoppingCart qshoppingCart = new ShoppingCart();
        qshoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingMapper.selectByAll(qshoppingCart);
    }

    /**
     * 小程序中添加菜品/套餐购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {

        // 新建一个ShoppingCart对象
        ShoppingCart queryshoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, queryshoppingCart);
        queryshoppingCart.setUserId(BaseContext.getCurrentId());

        log.info("[添加购物车] queryshoppingCart：{}", queryshoppingCart);
        // 查询queryshoppingCart对象所有的数据封装成列表
        List<ShoppingCart> shoppingCartList = shoppingMapper.selectByAll(queryshoppingCart);

        ShoppingCart dbshoppingCart = null;
        // 判断是否购物车内存在，如果存在更新购物车
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            dbshoppingCart = shoppingCartList.get(0);
            dbshoppingCart.setNumber(dbshoppingCart.getNumber() + 1);

            // 更新购物车数据
            shoppingMapper.updateShoppingById(dbshoppingCart);
        } else {
            // 购物车不存在，创建购物车
            // 判断当前是套餐还是菜品
            Long dishId = queryshoppingCart.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.selectDishById(dishId);
                queryshoppingCart.setName(dish.getName());
                queryshoppingCart.setImage(dish.getImage());
                queryshoppingCart.setAmount(dish.getPrice());
            } else {
                // 当前是套餐
                Setmeal setmeal = setmealMapper.findSetmealById(queryshoppingCart.getSetmealId());
                queryshoppingCart.setName(setmeal.getName());
                queryshoppingCart.setImage(setmeal.getImage());
                queryshoppingCart.setAmount(setmeal.getPrice());

            }
            // set公共
            queryshoppingCart.setNumber(1);
            queryshoppingCart.setCreateTime(LocalDateTime.now());
            log.info("[添加购物记录] shoppingCart:{}", queryshoppingCart);
            shoppingMapper.insertShoppingCart(queryshoppingCart);
        }

    }
}
